/*
 * 项目名称:    wms-server-jeecg
 * 创建人员:    gongwenkai
 * 创建时间:    2023-09-27
 */
package org.jeecg.modules.flow.callback.outbound.order;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.basic.material.entity.BasicMaterial;
import org.jeecg.modules.basic.material.service.IBasicMaterialService;
import org.jeecg.modules.business.doc.outbound.entity.WmsOutReceipt;
import org.jeecg.modules.business.doc.outbound.entity.WmsOutReceiptMaterial;
import org.jeecg.modules.business.doc.outbound.entity.vo.OutBoundReceiptSaveVO;
import org.jeecg.modules.business.doc.outbound.service.IWmsOutReceiptMaterialService;
import org.jeecg.modules.business.doc.outbound.service.IWmsOutReceiptService;
import org.jeecg.modules.business.outbound.entity.WmsOutReceiptTask;
import org.jeecg.modules.business.outbound.entity.WmsOutReceiptTaskMaterial;
import org.jeecg.modules.business.outbound.entity.WmsOutReceiptTaskPick;
import org.jeecg.modules.business.outbound.service.IWmsOutReceiptTaskMaterialService;
import org.jeecg.modules.business.outbound.service.IWmsOutReceiptTaskPickService;
import org.jeecg.modules.business.outbound.service.IWmsOutReceiptTaskService;
import org.jeecg.modules.flow.callback.enums.DocumentStatusEnum;
import org.jeecg.modules.flow.sys.common.BaseCustomBeanRun;
import org.jeecg.modules.flow.sys.entity.dto.BaseFlowDto;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 销售出库出库单 拣货备料 回调bean（已作废）
 *
 * @author gongwenkai
 * @version 1.0.0
 * @since 2023-09-27
 */
@Deprecated
@RequiredArgsConstructor
@Component
public class OutBoundReceiptXSCKSaveBean<T> extends BaseCustomBeanRun<T> {

    private final IWmsOutReceiptTaskService wmsOutReceiptTaskService;

    private final IWmsOutReceiptService wmsOutReceiptService;


    /**
     * 业务逻辑 销售出库出库单 拣货备料
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void actureRun() {
        BaseFlowDto baseFlowDto = this.getBaseFlowDto();
        // 获取请求参数
        baseFlowDto.translateClass(OutBoundReceiptSaveVO.class);
        OutBoundReceiptSaveVO saveVO= (OutBoundReceiptSaveVO)baseFlowDto.getData();

        WmsOutReceipt wmsOutReceipt = new WmsOutReceipt();
        BeanUtils.copyProperties(saveVO, wmsOutReceipt);
        String receiptId;
        String taskId=wmsOutReceipt.getWmsOutReceiptTaskId();
        if(StrUtil.isEmpty(wmsOutReceipt.getId())){
            receiptId = wmsOutReceiptService.saveMain(wmsOutReceipt, saveVO.getWmsOutReceiptMaterialList(),false);

        }else{
            receiptId=wmsOutReceipt.getId();
            WmsOutReceipt wmsOutReceiptEntity = wmsOutReceiptService.getById(receiptId);
            if (wmsOutReceiptEntity == null) {
                throw new JeecgBootException("未找到对应数据");
            }
            wmsOutReceiptService.updateMain(wmsOutReceipt, saveVO.getWmsOutReceiptMaterialList(),false);
        }
        // 更新出库任务状态为执行中
        wmsOutReceiptTaskService.update(new LambdaUpdateWrapper<WmsOutReceiptTask>()
                .set(WmsOutReceiptTask::getDocumentStatus, DocumentStatusEnum.PROCESS.getCode())
                .eq(WmsOutReceiptTask::getId,taskId)
        );
        // 当前前端调用业务需要你返回的
        baseFlowDto.setResult(Result.OK());
        // id是保存业务记录的id。用于生成业务流程信息记录
        baseFlowDto.setInfoId(receiptId);
    }
}
